#pragma once


#include<iostream>

using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTreeNode
{
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;

	RBTreeNode(const pair<K, V> kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{ }
};

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	void RotateR(Node* parent)
	{
		Node* sub_left = parent->_left;
		Node* sub_left_right = sub_left->_right;
		Node* parent_parent = parent->_parent;
		parent->_left = sub_left_right;
		if (sub_left_right)
		{
			sub_left_right->_parent = parent;
		}
		sub_left->_right = parent;
		parent->_parent = sub_left;
		parent_parent->_left = sub_left;
		if (parent_parent == nullptr)
		{
			_root = sub_left;
		}
		else
		{
			if (parent == parent_parent->_left)
			{
				parent_parent->_left = sub_left;
			}
			else
			{
				parent_parent->_right = sub_left;
			}
		}
		sub_left->_parent = parent_parent;
	}

	void RotateL(Node* parent)
	{
		Node* sub_right = parent->_right;
		Node* sub_right_left = sub_right->_left;
		Node* parent_parent = parent->_parent;
		parent->_right = sub_right_left;
		if (sub_right_left)
		{
			sub_right_left->_parent = parent->_right;
		}

	}

	bool insert(const pair<K, V> kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (kv.first > cur->_kv.first)
			{
				cur = cur->_right;
				parent = cur;
			}
			else if (kv.first < cur->_kv.first)
			{
				cur = cur->_left;
				parent = cur;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		cur->_col = RED;
		if (cur->_kv.first > parent->_kv.first)
		{
			cur = parent->_right;
		}
		else
		{
			cur = parent->_right;
		}

		while (parent && parent->_col == RED)
		{
			Node* parent_parent = parent->_parent;
			if (parent == parent_parent->_left)
			{
				Node* uncle = parent_parent->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					parent_parent->_col = RED;
					cur = parent_parent;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(parent_parent);
						parent->_col = BLACK;
						parent_parent->_col = RED;
					}
					else
					{

					}
				} 
			}
			
		}
	}

private:
	Node* _root = nullptr;
};

int main()
{


	return 0;
}